package com.kinon.norogamilk.util;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.kinon.norogamilk.R;
import com.kinon.norogamilk.application.MilkApp;
import com.youth.banner.Banner;

/**
 * Created by lhqq on 2017-12-22.
 */

public class MyImageLoader {

    private static MyImageLoader instance;

    public static MyImageLoader getInstance() {
        if (null == instance) {
            synchronized (MyImageLoader.class) {
                if (null == instance) {
                    instance = new MyImageLoader();
                }
            }
        }
        return instance;
    }

    public static DisplayMetrics getMetrics(Activity activity) {
        DisplayMetrics displaysMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displaysMetrics);
        return displaysMetrics;
    }

    public void loadBannerListImage(Activity activity, String url, ImageView imageView, Banner banner) {
        processImageView(activity, 750, 300, banner);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        getDrawableRequestBuilder(null, url, R.drawable.img_cow_paysuccess, R.drawable.img_cow_paysuccess).into(imageView);
    }

    private void processImageView(Activity activity, int intrinsicWidth, int intrinsicHeight, View view) {
        if (view.getContext() == null) {
            return;
        }
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        DisplayMetrics metrics = getMetrics(activity);
        if (layoutParams == null) {
            layoutParams = new ViewGroup.LayoutParams(metrics.widthPixels, (metrics.widthPixels * intrinsicHeight / intrinsicWidth));
        } else {
            layoutParams.width = metrics.widthPixels;
            layoutParams.height = (metrics.widthPixels * intrinsicHeight / intrinsicWidth);
        }
        view.setLayoutParams(layoutParams);
    }

    public synchronized DrawableRequestBuilder<String> getDrawableRequestBuilder(Context context, String url, int placeholderResId,
                                                                                 int errorResId) {
        if (!TextUtils.isEmpty(url)) {
            if (url.startsWith("https")) {
                String[] split = url.split("/");
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < split.length; i++) {
                    if (split[i].length() == 0) {
                        continue;
                    }
                    if (i == 0) {
                        builder.append(split[i]);
                        builder.append("//");
                    } else {
                        builder.append(split[i]);
                        builder.append("/");
                    }
                }
                url = builder.substring(0, builder.length() - 1);
            }
        }
        return Glide.with(MilkApp.getInstance()).load(url).placeholder(placeholderResId).error(errorResId)
                .listener(null).diskCacheStrategy(DiskCacheStrategy.SOURCE);
    }

}
